package com.xneure.database.sql.handler;

import java.util.HashMap;
import java.util.Map;
import com.xneure.database.metaobject.TableMetaObject;
import com.xneure.database.sql.ValueConvert;
import com.xneure.utils.ObjectUtil;

public abstract class SqlHandler{
	private static ValueConvert valueConvert = new ValueConvert();
	private static Map<Integer,TableMetaObject> metaObject = new HashMap<>();
	protected SqlAspect sqlAspect;

	public static TableMetaObject getMetaObject(Class<?> cls){
		synchronized(cls){
			TableMetaObject mo = metaObject.get(cls.hashCode());
			if(null == mo){
				mo = new TableMetaObject(cls);
				metaObject.put(cls.hashCode(),mo);
			}
			return mo;
		}
	}

	public static void setValueConvertClass(Class<? extends ValueConvert> valueConvertClass){
		if(valueConvertClass.equals(valueConvert.getClass()) == false){
			valueConvert = ObjectUtil.newInstance(valueConvertClass);
		}
	}

	public static ValueConvert getValueConvert(){
		return valueConvert;
	}

	public abstract boolean isSupport(HandlerType hType,Class<?> cls);

	public abstract void addParam(String name,Object value);

	public abstract String getSql();

	public abstract void addWhere(String where);

	public abstract void build(Object obj);

	public void resetParam(){
	}

	public static String toInSql(Object... c){
		StringBuilder string = new StringBuilder();
		for(Object s:c){
			string.append(getValueConvert().sqlValue(s)).append(',');
		}
		return string.substring(0,string.length() - 1);
	}

	public void setSqlAspect(SqlAspect sqlAspect){
		this.sqlAspect = sqlAspect;
	}
}
